perf: optimize dashboard refresh from >500ms to ~140ms#8
Merged
Conversation
Rewrite getToolUsageSummary() from N×5 dynamic queries to single prepared CTE query. Collapse getTokenSummary() from 4 queries to 1. Remove date() function wrapping in WHERE clauses to enable index usage. Add 2s TTL cache on /api/stats route. Move maintenance off request path to background timers. Add mmap_size/cache_size pragmas and idx_sessions_last_seen index.
bf10290 to
a78506f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getToolUsageSummary()from N×5 dynamic queries with string interpolation to a single prepared CTE query (P0, ~60-80% of latency)getTokenSummary()from 4 separate full-table scans to 1 query with conditional aggregation (P1)date()function wrapping in WHERE clauses to enable index usage onidx_messages_timestamp/idx_tool_calls_timestamp(P1)/api/statsroute to skip redundant DB queries within refresh cycles (P2)maybeAggregate()/maybeGC()from request path to backgroundsetIntervaltimers (P2)mmap_size(256MB) andcache_size(8MB) SQLite pragmas for read performance (P2)idx_sessions_last_seenindex via new migration v2 (P3)getTodayTokensstatement in constructor for consistencyTest coverage
Measured result
Dashboard auto-refresh latency reduced from >500ms (yellow) to ~140ms (green).